package com.ebeitech.auction.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.View;

import com.ebeitech.auction.R;
import com.ebeitech.auction.application.EbeiApplication;
import com.ebeitech.auction.constants.ActionConstants;
import com.ebeitech.auction.constants.CommonConstants;
import com.ebeitech.auction.domain.dao.UserDao;
import com.ebeitech.auction.http.EbeiErrorCode;
import com.ebeitech.auction.http.HttpService;
import com.ebeitech.auction.http.model.VersionModel;
import com.ebeitech.auction.http.service.HttpAuctionServiceRx;
import com.ebeitech.auction.push.xiaomi.PushUtils;
import com.ebeitech.auction.qqim.util.QQIMUtil;
import com.ebeitech.auction.view.homepage.LoginActivity;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.ui.dialog.CommonAlertDialogFragment;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.VersionUtil;
import com.ebeitech.library.util.ViewUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by alice on 2017/11/7.
 */
public class PublicUtil {

	public static void doLogout(final Context context) {
		Observable.create(new Observable.OnSubscribe<Boolean>() {

			@Override
			public void call(Subscriber<? super Boolean> subscriber) {
				UserDao.getInstance().setUserLogout(EbeiApplication.getUserAccount());
				QQIMUtil.getInstance().doLogout();
				PushUtils.sendMyStateUrl(context, SPConstants.LOGOUT_STATE);
				SPManager.getInstance().put(CommonConstants.IS_LOGIN_OFFLINE, true);

				subscriber.onNext(true);
			}
		}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Boolean>() {
			@Override
			public void onCompleted() {

			}

			@Override
			public void onError(Throwable e) {

			}

			@Override
			public void onNext(Boolean aBoolean) {
				Intent intent = new Intent();
				intent.setAction(ActionConstants.ACTION_EXIT_APP);
				context.sendBroadcast(intent);

				intent = new Intent(context, LoginActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("isLogout",true);
				EbeiApplication.getContext().startActivity(intent);
				((Activity) context).finish();
			}
		});
	}

	public static void showCheckVersionDialog(final Context context, final String downloadUrl, String versionName, String updateContent) {
		final FragmentManager fm;
		if (context instanceof FragmentActivity) {
			fm = ((FragmentActivity) context).getSupportFragmentManager();
		} else {
			fm = null;
		}
		if (fm == null) {
			return;
		}
		CommonAlertDialogFragment dialogFragment = ViewUtil.getAlertDialog(context, context.getString(R.string.ebei_new_version_x, versionName),
				updateContent, new CommonAlertDialogFragment.OnBtnClickListener() {
			@Override
			public void onClick(View v, CommonAlertDialogFragment dialog) {
				ViewUtil.dismissDialog(dialog);

				Intent intent = new Intent(context, VersionService.class);
				intent.putExtra("url", downloadUrl);
				context.startService(intent);
			}
		}, new CommonAlertDialogFragment.OnBtnClickListener() {
			@Override
			public void onClick(View v, CommonAlertDialogFragment dialog) {
				ViewUtil.dismissDialog(dialog);
			}
		}, context.getString(R.string.ebei_download_right_now), context.getString(R.string.ebei_download_later));

		dialogFragment.setMessageGravity(Gravity.LEFT);
		dialogFragment.show(fm, "CheckVersionDialog");
	}

	/**
	 * 检查版本更新
	 */
	public static void checkVersion(final Context context)
	{
		String versionName = VersionUtil.getVersionName(context);
		Map<String, String> params = new HashMap<String, String>();
		params.put("platform", "1");
		params.put("version", versionName);
		HttpService mHttpService = HttpService.getInstance();
		HttpAuctionServiceRx mHttpAuctionServiceRx = mHttpService.getRxGsonRetrofit().create(HttpAuctionServiceRx.class);
		final Call<ResponseBody> mCall = mHttpAuctionServiceRx.checkVersion(params);
		mHttpService.executeHttp(context, mCall, new HttpService.HttpListener<VersionModel>() {
			@Override
			public void onStart() {

			}

			@Override
			public void onError(EbeiErrorCode errorCode) {

			}

			@Override
			public void onSuccess(VersionModel baseModel) {
				if (baseModel.getData().isHasNewVersion())
				{
					PublicUtil.showCheckVersionDialog(context, baseModel.getData().get_$1().getUrl(), baseModel.getData().get_$1().getVersion(), baseModel.getData().get_$1().getUpdateDesc());
				}
			}
		}, VersionModel.class, false);

	}
}
